package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public int addUser(User user) {
        String sql="insert into user(user_name,discount,password,phone) values(?,?,?,?)";
        return executeUpdate(sql,user.getUserName(),user.getDiscount(),user.getPassword(),user.getPhone());
    }

    @Override
    public boolean findByPhone(String phone) {
        String  sql  ="SELECT * from user WHERE phone = ?";
        ResultSet rs = executeQuery(sql, phone);
        try {
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int updatePassword(String discount, String password, String phone) {
        String  sql = "update user set PASSWORD  = ? WHERE discount = ? and phone =?";
        return executeUpdate(sql,password,discount,phone);
    }

    @Override
    public User login(String discount, String password) {
        String sql="SELECT * FROM `user` WHERE `user`.discount =  ? AND `user`.`password` =  ?";
        ResultSet rs = executeQuery(sql, discount, password);
        try {
            if(rs.next()) {
                User user=new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setDiscount(rs.getString(3));
                user.setPassword(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setAddress(rs.getString(7));
                user.setMoney(rs.getDouble(8));
                user.setScore(rs.getInt(9));
                return 	user;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateUser(String headImg, String username, String address,int id) {
        String sql = "update user set head_img=?,user_name=?,address=? where id = ?";
        return super.executeUpdate(sql,headImg,username,address,id);
    }

    @Override
    public List<User> findAll() {
        String sql = "SELECT * FROM `user`";
        List<User> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql);
        try {
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setDiscount(rs.getString(3));
                user.setPassword(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setAddress(rs.getString(7));
                user.setMoney(rs.getDouble(8));
                user.setScore(rs.getInt(9));
                list.add(user);
            }
            return list;
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllByName(String name) {
        String sql = "SELECT * FROM `user` WHERE user_name LIKE ?";
        List<User> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql,"%"+name+"%");
        try {
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setDiscount(rs.getString(3));
                user.setPassword(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setAddress(rs.getString(7));
                user.setMoney(rs.getDouble(8));
                user.setScore(rs.getInt(9));
                list.add(user);
            }
            return list;
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int addAll(User user) {
        String sql = "INSERT INTO `user`(user_name,discount,`password`,phone,head_img,address，money,score) VALUES (?,?,?,?,?,?,?,?)";
        return executeUpdate(sql,user.getUserName(),user.getDiscount(),user.getPassword(),user.getPhone(),user.getHeadImg(),
                user.getAddress(),user.getMoney(),user.getScore());
    }

    @Override
    public int updateById(User user) {
        String sql = "UPDATE `user` SET user_name=?,discount=?,`password`=?,phone=?,head_img=?,address=?,money=?,score=? WHERE id=?";
        return executeUpdate(sql,user.getUserName(),user.getDiscount(),user.getPassword(),user.getPhone(),user.getHeadImg(),
                user.getAddress(),user.getMoney(),user.getScore(),user.getId());
    }

    @Override
    public int delById(int id) {
        String sql = "DELETE FROM `user` WHERE id=?";
        return executeUpdate(sql,id);
    }

    @Override
    public User findId(int id) {
        String sql = "SELECT * FROM `user` WHERE id = ?";
        List<User> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql,id);
        try {
           if (rs.next()){
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setDiscount(rs.getString(3));
                user.setPassword(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setAddress(rs.getString(7));
                user.setMoney(rs.getDouble(8));
                user.setScore(rs.getInt(9));
               return user;
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;
    }
}


